<form id="bcfgListForm" name="bcfgListForm" method="POST" action="#" class="well well-large span7">
    <fieldset>
        <legend>Конфигурация ботов</legend>
    </fieldset>

<?php
$e = Arr::getValue($params, 'error', false);

if ($e) {?>
<div class="alert alert-danger"> <?php echo $e; ?> </div>
<?php } ?>




<?php

$files = Arr::getValue($params, 'files', array());
$tableHtml = '';

foreach ($files as $key=>$item) {
    $lm = $item['lastmod']? $item['lastmod'] : 'N/A';
    $sz = $item['size']? $item['size'] : 'N/A';
    
    $tableHtml .= <<<__HTML__
    <tr id="bcfg-tr-{$key}">
        <td>
            <button type="button" class="btn btn-info btn-small" id="bcfgBtnEdit-{$key}" onclick="bcfgEditFile(this); return false;">
                <i class="icon-edit icon-white"></i>
            </button>
        </td>
        <td data-field="file">{$item['file']}</td>
        <td data-field="lastmod">{$lm}</td>
        <td data-field="size">{$sz}</td>
    </tr>
__HTML__;
}


?>







<table class="table table-striped table-bordered table-condensed" id="bcfgListTable">
    <thead>
        <tr style="background: #e0e0e0;">
            <th style="width:30px;">&nbsp;</th>
            <th>Файл</th>
            <th>Изменён</th>
            <th>Размер, байт</th>
        </tr>
    </thead>
    <tbody>
        <?php echo $tableHtml;?>
    </tbody>
</table>

</form>

<?php 
    echo includeFile(APP_PAGE_PARTS.'forms/form_edit_text.php'); 
?>

<script>
    function bcfgEditFile(elm) {
        var name = elm.id.gReplace('bcfgBtnEdit-','');
        
        jsonRequest(
            'botconfig.php',
            {
                file: name,
                action: '<?php echo ACTIONS::BOTCONFIG_GETTEXT; ?>'
            },
            function(data) {
                if(!checkSession(data)) {
                    Elm.loginFormModal.dialog('open');
                    Elm.loginFormModal.successCallback = function(){ bcfgEditFile(elm); };
                    return;
                }

                if (!data.state) {
                    uiAlert(data.message,'Error');
                    return;
                }

                var resp = arrayGetValue(data, 'response_data', false);
                var content = arrayGetValue(resp, 'content', false);
                
                if (content === false) {
                    uiAlert('Ошибка на сервере.','Error');
                    return;
                }
                if (!content) content = '';
                etxtDialogOpen(
                        {title:name+'.xml', text: content}, 
                        function(text){ 
                            bcfgSaveText(name, text);
                        }, 
                        function(){}
                );    
            },

            function() {
                uiAlert('Ошибка соединения','Error');
            }
        );
        
        
    }
    
    
    function bcfgSaveText(name, text) {
        jsonRequest(
            'botconfig.php',
            {
                file: name,
                text: text,
                action: '<?php echo ACTIONS::BOTCONFIG_SAVETEXT; ?>'
            },
            function(data) {
                if(!checkSession(data)) {
                    Elm.loginFormModal.dialog('open');
                    Elm.loginFormModal.successCallback = function(){  };
                    return false;
                }

                if (!data.state) {
                    uiAlert(data.message,'Error');
                    return false;
                }

                var resp = arrayGetValue(data, 'response_data', false);
                var saved = arrayGetValue(resp, 'saved', false);
                
                if (saved === false) {
                    uiAlert('Ошибка на сервере.','Error');
                    return false;
                }
                
                refreshFileInfo(saved);
                etxtDialogClose();
                return true;
            },

            function() {
                uiAlert('Ошибка соединения','Error');
            }
        );
    }
    
    
    function refreshFileInfo(info) {
        var lm = arrayGetValue(info, 'lastmod', 'N/A');
        var sz = arrayGetValue(info, 'size', 'N/A');
        var fl = arrayGetValue(info, 'file', false);
        
        if (!fl) return;
        var f = fl.gReplace('.xml', '');
        
        var elm = $('#bcfg-tr-'+f);
        if (!elm) return;
        
        elm.find('td[data-field="file"]').html(fl);
        elm.find('td[data-field="lastmod"]').html(lm);
        elm.find('td[data-field="size"]').html(sz);
    }
    
    
    
    
    
    
    
</script>